package com.draven.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.draven.common.BusinessException;
import com.draven.entity.Note;
import com.draven.mapper.NoteMapper;
import com.draven.service.NoteService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 笔记服务实现类
 */
@Service
public class NoteServiceImpl extends ServiceImpl<NoteMapper, Note> implements NoteService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long add(Long userId, String title, String content, String category, String tags) {
        // 创建笔记
        Note note = new Note();
        note.setUserId(userId);
        note.setTitle(title);
        note.setContent(content);
        note.setCategory(category);
        note.setTags(tags);

        save(note);
        return note.getId();
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long add(Note note) {
        if (StringUtils.isBlank(note.getTitle())) {
            throw new BusinessException(400, "标题不能为空");
        }
        save(note);
        return note.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Long id, Long userId, String title, String content, String category, String tags) {
        // 查询笔记
        Note note = getDetail(id, userId);

        // 更新信息
        note.setTitle(title);
        note.setContent(content);
        note.setCategory(category);
        note.setTags(tags);

        updateById(note);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Note note) {
        if (note.getId() == null) {
            throw new BusinessException(400, "ID不能为空");
        }
        if (StringUtils.isBlank(note.getTitle())) {
            throw new BusinessException(400, "标题不能为空");
        }
        
        // 查询笔记并验证所有权
        Note existingNote = getDetail(note.getId(), note.getUserId());
        
        // 更新信息
        note.setCreateTime(existingNote.getCreateTime());
        updateById(note);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id, Long userId) {
        // 查询笔记
        Note note = getDetail(id, userId);

        // 删除笔记
        removeById(id);
    }

    @Override
    public Note getDetail(Long id, Long userId) {
        // 查询笔记
        Note note = getById(id);
        if (note == null) {
            throw new BusinessException(400, "笔记不存在");
        }

        // 校验所有权
        if (!note.getUserId().equals(userId)) {
            throw new BusinessException(403, "无权限操作");
        }

        return note;
    }

    @Override
    public Page<Note> page(Long userId, String title, String category, String tag, Integer pageNum, Integer pageSize) {
        // 构建查询条件
        LambdaQueryWrapper<Note> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Note::getUserId, userId);
        wrapper.like(StringUtils.isNotBlank(title), Note::getTitle, title);
        wrapper.eq(StringUtils.isNotBlank(category), Note::getCategory, category);
        // 标签查询
        if (StringUtils.isNotBlank(tag)) {
            wrapper.like(Note::getTags, tag);
        }
        wrapper.orderByDesc(Note::getCreateTime);

        // 分页查询
        return page(new Page<>(pageNum, pageSize), wrapper);
    }
    
    @Override
    public int countByUserId(Long userId) {
        LambdaQueryWrapper<Note> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Note::getUserId, userId);
        return (int) count(wrapper);
    }
} 